package com.spring.hot;

import jdk.jfr.DataAmount;

/**
 * @author Cizai
 * @since 2022/12/02 16:55
 * @see <a href="https://leetcode.cn/problems/merge-two-sorted-lists/?favorite=2cktkvj">...</a>
 **/

public class 合并两个有序链表 {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

        ListNode result = new ListNode(-1);
        ListNode pre = result;

        while (list1!=null && list2 != null) {
            if (list1.val > list2.val) {
                pre.next = new ListNode(list2.val);
                pre = pre.next;
                list2 = list2.next;
            }else {
                pre.next = new ListNode(list1.val);
                list1 = list1.next;
                pre = pre.next;
            }
        }
        if (list1 == null) {
            pre.next = list2;
        }
        if (list2 == null) {
            pre.next = list1;
        }

        return result.next;
    }
}
